package com.common.base.security;


import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * [简要描述]:AES加解密<br/>
 * [详细描述]:<br/>
 *
 * @author sosou
 * @version 1.0, 2017年12月21日
 * @since
 */
public class AESEncrypt {

    public static final String DS_AES_IV = "ky2#FD9J+x0Dm*i4";

    private static final String encryptKey = "ztAtqrsGCPf2qDTW";
    private static final String encryptIv = "e5V8nhM72zAxXFcS";

    /**
     * 加密
     *
     * @param content  需要加密的内容
     * @param password 加密密码
     * @return
     */
    public static byte[] encrypt(String content, String password) {

        if (TextUtils.isEmpty(password)) {
            return null;
        }

        try {
//            SecretKeySpec key = new SecretKeySpec(Arrays.copyOf(password.getBytes("utf-8"), 16), "AES");
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(DS_AES_IV.getBytes("utf-8")));
            byte[] result = cipher.doFinal(byteContent);
            return result; // 加密
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加密
     *
     * @param content  需要加密的内容
     * @return
     */
    public static byte[] encrypt(String content) {

        try {
            SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(encryptIv.getBytes("utf-8")));
            byte[] result = cipher.doFinal(byteContent);
            return result; // 加密
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加密
     *
     * @param content  需要加密的内容
     * @return
     */
    public static byte[] encryptIv(String content,String encryptKeyTemp,String encryptIvTemp) {

        try {
            SecretKeySpec key = new SecretKeySpec(encryptKeyTemp.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(encryptIvTemp.getBytes("utf-8")));
            byte[] result = cipher.doFinal(byteContent);
            return result; // 加密
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param content  待解密内容
     * @param password 解密密钥
     * @return
     */
    public static byte[] decrypt(byte[] content, String password) {
        try {
//            SecretKeySpec key = new SecretKeySpec(Arrays.copyOf(password.getBytes("utf-8"), 16), "AES");
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(DS_AES_IV.getBytes()));
            byte[] result = cipher.doFinal(content);
            return result; // 加密
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
//        catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 解密
     *
     * @param content  待解密内容
     * @param encryptKeyTemp 解密密钥
     * @return
     */
    public static byte[] decryptVi(byte[] content,String encryptKeyTemp,String encryptIvTemp) {

        try {
//            SecretKeySpec key = new SecretKeySpec(Arrays.copyOf(password.getBytes("utf-8"), 16), "AES");
            SecretKeySpec key = new SecretKeySpec(encryptKeyTemp.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(encryptIvTemp.getBytes()));
            byte[] result = cipher.doFinal(content);
            return result; // 加密
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
//        catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decode2String(String content, String key) {

        if (content == null) {
            return "";
        }

        //解密返回的数据
        byte[] bytedata = Base64Utils.decode(content);

        if (bytedata == null) {
            return "";
        }

        byte[] afterdecrypt = AESEncrypt.decrypt(bytedata, key);
        String result = "UnsupportedEncodingException";

        if (afterdecrypt == null) {
            return "";
        }
        if (afterdecrypt.length == 0) {
            return "";
        }

        try {
            result = new String(afterdecrypt, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static String decode2StringVi(String content, String encryptKeyTemp,String encryptIvTemp) {

        if (content == null) {
            return "";
        }

        if (TextUtils.isEmpty(encryptKeyTemp) || TextUtils.isEmpty(encryptIvTemp)){
            return content;
        }

        //解密返回的数据
        byte[] bytedata = Base64Utils.decode(content);

        if (bytedata == null) {
            return "";
        }

        byte[] afterdecrypt = AESEncrypt.decryptVi(bytedata, encryptKeyTemp,encryptIvTemp);
        String result = "UnsupportedEncodingException";

        if (afterdecrypt == null) {
            return "";
        }
        if (afterdecrypt.length == 0) {
            return "";
        }

        try {
            result = new String(afterdecrypt, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String encode2String(String content, String key) {

        if (TextUtils.isEmpty(key)) {
            return "";
        }

        byte[] bytes = encrypt(content, key);
        return Base64Utils.encode(bytes);
    }

    public static String encode2String(String content) {
        byte[] bytes = encrypt(content);
        return Base64Utils.encode(bytes);
    }

    public static String encode2StringVi(String content,String encryptKeyTemp,String encryptIvTemp) {

        if (TextUtils.isEmpty(encryptKeyTemp) || TextUtils.isEmpty(encryptIvTemp)){
            return content;
        }

        byte[] bytes = encryptIv(content,encryptKeyTemp,encryptIvTemp);
        return Base64Utils.encode(bytes);
    }
}
